﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;
using umbraco.BusinessLogic.Actions;
using System.Text;
using Procure.Data.Context;

namespace Procure.Web.order
{
    public class loadOrder : BaseTree
    {
        public loadOrder(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = TreeAlias;
            rootNode.NodeID = "init";

            rootNode.Action = "javascript:openDashboard('procure');";
        }

        protected override void CreateRootNodeActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionRefresh.Instance);
        }

        protected override void CreateAllowedActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionRefresh.Instance);
        }

        public override void Render(ref XmlTree tree)
        {
            try
            {
                if (this.id == -1) // root
                    RenderTop(ref tree);
                else if (this.id > 0)
                    RenderOrders(ref tree);
            }
            catch (Exception) { }
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            RenderScreensJS(Javascript);
        }

        #region RenderTop
        private void RenderTop(ref XmlTree tree)
        {
            ProcureDataContext dataContext = DataFactory.GetDataContext();
            var statusList = from status in dataContext.Status
                             where status.VisibleInAdmin == true
                             orderby status.SortOrder
                             select status;

            foreach (Status status in statusList)
            {
                XmlTreeNode xNode = XmlTreeNode.Create(this);
                xNode.NodeID = status.Id.ToString();
                xNode.Text = status.Name;
                xNode.Menu = new List<IAction>(new IAction[] { ActionRefresh.Instance, ActionNew.Instance });
                xNode.HasChildren = true;
                xNode.Source = GetTreeServiceUrl(xNode.NodeID);

                xNode.Icon = ".sprTreeFolder";
                xNode.OpenIcon = ".sprTreeFolder_o";

                base.OnBeforeNodeRender(ref tree, ref xNode, EventArgs.Empty);
                tree.Add(xNode);
                base.OnAfterNodeRender(ref tree, ref xNode, EventArgs.Empty);
            }
        }
        #endregion

        #region RenderOrders
        private void RenderOrders(ref XmlTree tree)
        {
            int statusId = this.id;

            ProcureDataContext dataContext = DataFactory.GetDataContext();
            var orders = from order in dataContext.Orders
                         where order.StatusId == statusId
                         orderby order.OrderedOn
                         select order;
            XmlTreeNode xNode = null;

            foreach (Order order in orders)
            {
                xNode = XmlTreeNode.Create(this);
                xNode.NodeID = order.Id.ToString();

                xNode.Text = order.User.Username;
                xNode.Menu = new List<IAction>(new IAction[] { ActionDelete.Instance });
                xNode.Action = "javascript:openOrder(" + order.Id.ToString() + ");";
                xNode.HasChildren = false;

                xNode.Icon = "Order.gif";
                xNode.OpenIcon = "Order.gif";
                base.OnBeforeNodeRender(ref tree, ref xNode, EventArgs.Empty);
                tree.Add(xNode);
                base.OnAfterNodeRender(ref tree, ref xNode, EventArgs.Empty);
            }
        }
        #endregion

        #region RenderScreensJS
        private void RenderScreensJS(StringBuilder Javascript)
        {
            if (!string.IsNullOrEmpty(this.FunctionToCall))
            {
                Javascript.Append("function openOrder(id) {\n");
                Javascript.Append(this.FunctionToCall + "(id)\n");
                Javascript.Append("}\n");
            }
            else
            {
                Javascript.Append(
                    @"
function openOrder(id) {
	parent.right.document.location.href = 'Procure/editOrder.aspx?id=' + id;
}
");
            }
        }
        #endregion
    }
}